﻿
// Doi 1 so thanh 1 chu
function so1(s)
{
    var str="";
    var i=eval(s);
    switch (i){
        case 0: {break;}
        case 1: {str="một";break;}
        case 2: {str="hai";break;}
        case 3: {str="ba";break;}
        case 4: {str="bốn";break;}
        case 5: {str="năm";break;}
        case 6: {str="sáu";break;}
        case 7: {str="bảy";break;}
        case 8: {str="tám";break;}
        case 9: {str="chín";break;}
    }
    return str;
}
function soChuc(s)
{
    var str="";
    var i=eval(s);
    switch (i){
        case 0: {str="không";break;}
        case 1: {str="mười";break;}
        case 2: {str="hai";break;}
        case 3: {str="ba";break;}
        case 4: {str="bốn";break;}
        case 5: {str="năm";break;}
        case 6: {str="sáu";break;}
        case 7: {str="bảy";break;}
        case 8: {str="tám";break;}
        case 9: {str="chín";break;}
    }
    return str;
}
function so20(s)
{
    var str="";
    var i=eval(s);
    switch (i){
        case 0: {break; }
        case 1: {str="mốt";break;}
        case 2: {str="hai";break;}
        case 3: {str="ba";break;}
        case 4: {str="bốn";break;}
        case 5: {str="lăm";break;}
        case 6: {str="sáu";break;}
        case 7: {str="bảy";break;}
        case 8: {str="tám";break;}
        case 9: {str="chín";break;}
    }
    return str;
}
function so10(s)
{
    var str="";
    var i=eval(s);
    switch (i){
        case 0: {break; }
        case 1: {str="một";break;}
        case 2: {str="hai";break;}
        case 3: {str="ba";break;}
        case 4: {str="bốn";break;}
        case 5: {str="lăm";break;}
        case 6: {str="sáu";break;}
        case 7: {str="bảy";break;}
        case 8: {str="tám";break;}
        case 9: {str="chín";break;}
    }
    return str;
}
//Doc 2 so
function doc2(s){
    var str="2so";
    //alert(s);
    var ch=s.substring(0,1);
    if(ch=="0")
        s=s.substring(1);
    if(s.length==2){
        var chuc=s.substring(0,1);
        var donVi=s.substring(1,2);
        var nchuc=eval(chuc);
        var ndonVi=eval(donVi);
        if(nchuc==1){
            str=soChuc(chuc)+" ";
            str+=so10(donVi);//alert(str);
        }
        else{
            str=so1(chuc);
            str+=" mươi ";
            str+=so20(donVi);
        }
    }
    else if(s.length==1)
        str=so1(s);
    return str;
}
//Doc 3 so
function doc3(s){
    var str="3so";
    var ch=s.substring(0,1);//alert(ch);
    if(ch=="0")
        s=s.substring(1);
    if(s.length<3){
        str=doc2(s);
    }
    else if(s.length==3){
        var tram=s.substring(0,1);
        var chuc=s.substring(1,2);
        var donVi=s.substring(2,3);
        str=so1(tram);
        str+=" trăm";//alert(chuc);
        if(chuc==0){
            if(donVi!=0){
                str+=" lẻ ";
                str+=so1(donVi);
            }
        }
        else{
            str+=" ";
            var nso=s.substring(1,3);
            str+=doc2(nso);
        }
    }
    return str;
}
//Doi ra chu
function doiSo(s){
    var str="";
    var temp="";
    var len=s.length;
    //alert(len);
    if(len<=12){
        if(len>9){
            var ch=s.substring(0,len-9);
            if(ch!=0){
                temp=doc3(ch)+" tỉ";
                str=temp;
            }
        }
        if(len>6){
            var begin=0;
            var end;
            if(len>9)
                begin=len-9;
            end=len-6;
            var ch=s.substring(begin,end);
            //var nch=eval(ch);
            if(ch!=0){
                temp=doc3(ch)+" triệu";
                str+=" "+temp;
            }
        }
        if(len>3){
            var begin=0;
            var end;
            if(len>6)
                begin=len-6;
            end=len-3;
            var ch=s.substring(begin,end);
            //var nch=eval(ch);
            if(ch!=0){
                temp=doc3(ch)+" nghìn";
                str+=" "+temp;
            }
        }
        var begin=0;
        if (len>3)
            begin=len-3;
        var ch=s.substring(begin,len);
        if(ch!=0)
            str+=" "+doc3(ch);
        
    }
    else
        str="Số lớn hơn 999 tỉ";
    return str;
}
//tra ve chuoi tien
function bangChu(soTien) {
    if (eval(soTien) > 0) {
        var str = "";
        var num = Math.round(eval(soTien.replace(/,/g, '')));
        var dong = num + "";
        str = doiSo(dong);

        if (str.length > 2) {
            if (str.substring(0, 1) == " ")
                str = str.substring(1);
            var str1 = str.substring(0, 1);
            str1 = str1.toUpperCase(); //alert(" chu dau "+str1);
            str = str1 + str.substring(1);
        }
        str = str + " đồng";
        //document.getElementById("Realty_lblBangChu").innerHTML=str;
        //alert(str);
    }
    else {
        if (eval(soTien) == 0) {
            str = "Không đồng";
        }
        else {
            str = "Số không được âm";
        }        
    }
    
    return str;
}
function init(){
    var str=document.getElementById("Realty_txtPrice").value;
    if(str!="")
        bangChu(str);
    else
        document.getElementById("Realty_lblBangChu").innerHTML="Chưa có";
}